package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.codec.digest.DigestUtils;

import entity.Mogujie_user;
import service.Mogujie_userService;
import service.Impl.Mogujie_userServiceImpl;

/**
 * Servlet implementation class Mogujie_userServlet
 */
@WebServlet("/Mogujie_userServlet")
public class Mogujie_userServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       Mogujie_userService mogujie_user = null;
	@Override
	public void init() throws ServletException {
		// TODO Auto-generated method stub
		mogujie_user = new Mogujie_userServiceImpl();
		super.init();
	}
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Mogujie_userServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		//设置响应内容的类型并制定字符集,以免游览器展示中文内容时出现乱码
		response.setContentType("text/html;charset=UTF-8");

		HttpSession session = request.getSession();

		PrintWriter out = response.getWriter();
		String opr = request.getParameter("opr");
		System.out.println(opr);
		if(opr.equals("enter")) {
			Mogujie_user mogu = new  Mogujie_user();
			String name = request.getParameter("number");
			String pass = request.getParameter("password");
			System.out.println("name"+ name);
			System.out.println("pass" + pass);
			mogu = mogujie_user.searchInfo(name, pass);
			
			if (mogu != null) {
				out.print(mogu.getUserName());
				System.out.println(mogu.getImage());
				request.setAttribute("img", mogu.getImage());
			}else {
				out.print("用户不存在,或密码不正确");
			}
		}else if(opr.equals("reg")) {
			Integer  integral = 0;														//用户积分默认为0
			String phone = request.getParameter("phone");			//电话号码
			String uname = request.getParameter("uname");		//昵称
			String sex = request.getParameter("sex");					//性别
			String carid = request.getParameter("carid");				//身份证
			String password = request.getParameter("password");	//密码	
			password = DigestUtils.md5Hex(password);					//加密之后的密码
			Mogujie_user mogu = new Mogujie_user(uname,password,phone,0,carid,sex,integral);
			int num = mogujie_user.addUser(mogu);
			if(num > 0) {
				out.print("<script type='text/javascript'>"
	    				+"alert('新增成功!');"
	    				+"location.href='index.jsp';"
	    				+"</script>");
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
